#!/bin/sh
#desc: Create wrapper scripts
#package:oddiscovery
#type:reserved


# Copyright(c) 2011 OpenDomo Services SL. Licensed under GPL v3 or later

if test -z "$1"
then
   echo "Usage: $0 [conf file]"
   exit 0
fi

CNF_FILE=$1
DSCVPATH=/usr/local/opendomo/services
WRAPPERPATH=/usr/local/opendomo
CGIPATH=/etc/opendomo/root
EXECREMOTE="/usr/local/opendomo/discovery/execRemoteScript.sh"


# Remove old services
find /etc/opendomo/root/ -type l -name *.sh -exec rm {} \;
rm -f /usr/local/opendomo/*.sh

# Create new services
cat $CNF_FILE | grep -v "#service" | grep "service" | while read LINE
do
	# Get vars
	SCRIPT=`echo $LINE | cut -d ' ' -f2`
	USER=`echo $LINE | cut -d ' ' -f3`
	GROUP=`echo $LINE | cut -d ' ' -f4`
	PRIV=`echo $LINE | cut -d ' ' -f5`
	FPRIV=`echo $PRIV | sed -e 's/rwx/7/g' -e 's/r-x/5/g' -e 's/r--/4/g' -e 's/--x/1/g' -e's/---/0/g' -e's/-//g'`
	TYPE=`echo $LINE | cut -d ' ' -f6`
	CLASS=`echo $LINE | cut -d ' ' -f7`
	HOST=`echo $LINE | cut -d ' ' -f8`
	DESC=`grep '\#desc:' $DSCVPATH/$CLASS/$SCRIPT | cut -d ':' -f2 | head -1`
	NAME=`grep '\#name:' $DSCVPATH/$CLASS/$SCRIPT | cut -d ':' -f2 | head -1`
	#FIXME Dirty workaround: it should have the unique hostname or "localhost"

	# We can avoid the output sending a "mute" in the 2nd parameter
	if test "$2" != "mute"; then
		echo "SCRIPT: $SCRIPT"
		echo "USER: $USER"
		echo "GROUP: $GROUP"
		echo "PRIV: $PRIV (0$FPRIV)" 
		echo "TYPE: $TYPE"
		echo "CLASS: $CLASS"
		echo "HOST: $HOST"
		echo "DESC: $DESC"
		echo "NAME: $NAME"
		echo
	fi

	if test -z "$NAME"
	then
		NAME=$DESC
	fi


   # Create class if not exists
   if [ "$CLASS" != "syscript" ]
   then
      install -d $CGIPATH/$CLASS
   fi

	if test -L $WRAPPERPATH/$SCRIPT; then
		rm $WRAPPERPATH/$SCRIPT
	fi

	case "$TYPE" in
	local) # ------------------------------------------------------------------
		# Local services are executed in the agent the user wants.
		# There's a menu option for each service-agent pair.
		# ----------------------------------------------------------------
		if test "$HOST" = "localhost" 
		then
			echo '#!/bin/sh' > $WRAPPERPATH/$SCRIPT
			echo '# Wrapper script' >> $WRAPPERPATH/$SCRIPT
			echo "#desc:$DESC" >> $WRAPPERPATH/$SCRIPT
			echo "#name:$NAME" >> $WRAPPERPATH/$SCRIPT
			echo ". $DSCVPATH/$CLASS/$SCRIPT" >> $WRAPPERPATH/$SCRIPT
			ln -fs $WRAPPERPATH/$SCRIPT  $CGIPATH/$CLASS/$SCRIPT
			echo "$NAME [ ]" > $CGIPATH/$CLASS/$SCRIPT.name
		else
      	RUID=`$EXECREMOTE $HOST 'cat /etc/opendomo/uid'`
			echo "$EXECREMOTE $HOST \"ln -sf $DSCVPATH/$CLASS/$SCRIPT \
			/tmp/${RUID}_$SCRIPT && /tmp/${RUID}_$SCRIPT \$@\"" \
			> $WRAPPERPATH/${RUID}_$SCRIPT
			chmod +x $WRAPPERPATH/${RUID}_$SCRIPT
		 	ln -fs $WRAPPERPATH/${RUID}_$SCRIPT  $CGIPATH/$CLASS/${RUID}_$SCRIPT
	
			if test -z "$NAME"	
			then
				NAME=$SCRIPT
			fi
			echo "$NAME [ ($RUID)]" > $CGIPATH/$CLASS/${RUID}_$SCRIPT.name
		fi
	;;

	redundant) # --------------------------------------------------------------
		# Redundant services are executed in the first agent that offers them. 
		# If it fails, we try with the second.
		# ----------------------------------------------------------------
		if test "$HOST" = "localhost" 
		then
			# processService.sh process 'localhost' services first.
			rm -f $WRAPPERPATH/$SCRIPT
			touch  $WRAPPERPATH/$SCRIPT
			chmod ug+x $WRAPPERPATH/$SCRIPT

			echo '#!/bin/sh' >> $WRAPPERPATH/$SCRIPT
			echo '# Wrapper script' >> $WRAPPERPATH/$SCRIPT
			echo "#desc:$DESC" >> $WRAPPERPATH/$SCRIPT
			echo "#name:$NAME" >> $WRAPPERPATH/$SCRIPT

			echo  "$DSCVPATH/$CLASS/$SCRIPT \$@ && exit 0" \
				>> $WRAPPERPATH/$SCRIPT
			ln -fs $WRAPPERPATH/$SCRIPT  $CGIPATH/$CLASS/$SCRIPT
			echo "$NAME" > $CGIPATH/$CLASS/$SCRIPT.name
		else
			# Next, we add the remote services
			echo "$EXECREMOTE $HOST '$DSCVPATH/$CLASS/$SCRIPT \$@' && exit 0" \
				>> $WRAPPERPATH/$SCRIPT
			chmod +x $WRAPPERPATH/$SCRIPT
			echo "$NAME" > $CGIPATH/$CLASS/$SCRIPT.name
		fi
	;;

	multiple) # ---------------------------------------------------------------
		# All services are executed
		# ----------------------------------------------------------------
		if test "$HOST" = "localhost" 
		then
			rm -f $WRAPPERPATH/$SCRIPT
			touch  $WRAPPERPATH/$SCRIPT
			chmod ug+x $WRAPPERPATH/$SCRIPT

			echo '#!/bin/sh' >> $WRAPPERPATH/$SCRIPT
			echo '# Wrapper script' >> $WRAPPERPATH/$SCRIPT
			echo "#desc:$DESC" >> $WRAPPERPATH/$SCRIPT
			echo "#name:$NAME" >> $WRAPPERPATH/$SCRIPT

			echo  "$DSCVPATH/$CLASS/$SCRIPT \$@; 
         if test -z \"\$1\"; then exit 0; fi" \
			>> $WRAPPERPATH/$SCRIPT
			echo "$NAME" > $CGIPATH/$CLASS/$SCRIPT.name
		else
			echo "$EXECREMOTE $HOST \"ln -sf $DSCVPATH/$CLASS/$SCRIPT \
			/tmp/$SCRIPT && /tmp/$SCRIPT \$@\"" \
			>> $WRAPPERPATH/$SCRIPT

			echo "$NAME" > $CGIPATH/$CLASS/$SCRIPT.name
		fi
		chmod +x $WRAPPERPATH/$SCRIPT
		ln -s $WRAPPERPATH/$SCRIPT $CGIPATH/$CLASS/$SCRIPT
	;;
	esac

	## Aplicamos al wrapper los mismos privilegios que el script real
	if test -e "$WRAPPERPATH/$SCRIPT"
	then
		chown $USER:$GROUP $WRAPPERPATH/$SCRIPT
		chmod $FPRIV $WRAPPERPATH/$SCRIPT
	fi

done



